package com.zoo.diannaopeijian.service.impl;

import com.github.pagehelper.PageHelper;
import com.zoo.diannaopeijian.domain.Staff;
import com.zoo.diannaopeijian.domain.StaffExample;
import com.zoo.diannaopeijian.mapper.StaffMapper;
import com.zoo.diannaopeijian.service.StaffService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zx583 on 2018-01-08.
 */
@Service
public class StaffServiceImpl implements StaffService {
    @Resource
    private StaffMapper staffMapper;

    @Override
    public List<Staff> getStaff(Staff staff) {
        StaffExample example = new StaffExample();

        if (staff != null) {
            StaffExample.Criteria criteria = example.createCriteria();

            if (staff.getStaffid() != null) {
                criteria.andStaffidEqualTo(staff.getStaffid());
            }

            if (!StringUtils.isEmpty(staff.getStaffname())) {
                criteria.andStaffnameEqualTo(staff.getStaffname());
            }
        }

        return staffMapper.selectByExample(example);
    }

    @Override
    public List<Staff> getStaffWithPage(Integer page, Integer rows, Staff staff) {
        PageHelper.startPage(page, rows);

        return getStaff(staff);
    }

    @Override
    public List<Staff> getFuzzyStaff(Staff staff) {
        return staffMapper.selectFuzzyStaff(staff);
    }

    @Override
    public List<Staff> getFuzzyStaffWithPage(Integer page, Integer rows, Staff staff) {
        PageHelper.startPage(page, rows);

        return getFuzzyStaff(staff);
    }

    @Override
    public boolean addStaff(Staff staff) {
        return staffMapper.insertSelective(staff) == 1;
    }

    @Override
    public boolean deleteStaff(Integer staffid) {
        return staffMapper.deleteByPrimaryKey(staffid) == 1;
    }

    @Override
    public boolean updateStaff(Staff staff) {
        return staffMapper.updateByPrimaryKeySelective(staff) == 1;
    }

    @Override
    public String getStaffSalt(Integer staffid) {
        return staffMapper.selectSaltByPrimaryKey(staffid);
    }
}
